var utils = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    date: '',
    productName: '',
    buyNumber: 1,
    minDisabled: true,
    plusDisabled: false,
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    name: '',
    idcard: '',
    phone: ''
  },
  onLoad: function (options) {
    var that = this;
    let start = utils.formatTime(new Date()).split(' ')[0].replace(/\//g, '-');
    this.setData({
      id: options.id,
      start: start,
      date: start,
      end: '',
      productName: decodeURI(options.projectName)
    });
  },
  onShareAppMessage: function (res) {
    console.log(res)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '桶哥桶哥桶哥',
      path: '/pages/shoppingMall/shopMallProductDetail/productDetail',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  numMinus: function () {
    let that = this;
    let buyNumber = parseInt(that.data.buyNumber);
    if (buyNumber <= 1) {
      that.setData({
        minDisabled: true,
        plusDisabled: false,
      })
    } else {
      buyNumber -= 1;
      that.setData({
        buyNumber: buyNumber,
        minDisabled: false
      })
    }
  },
  numAdd: function () {
    let that = this;
    let buyNumber = parseInt(that.data.buyNumber) + 1;
    that.setData({
      buyNumber: buyNumber,
      minDisabled: false
    })
  },
  enterName: function (e) {
    this.setData({
      name: e.detail.value.trim()
    });
  },
  enterPhone: function (e) {
    this.setData({
      phone: e.detail.value.trim()
    });
  },
  enterIdcard: function (e) {
    this.setData({
      idcard: e.detail.value.trim()
    });
  },
  submitOrder: function (res) {
    let that = this;
    let telreg = /^(0|86|17951)?(13[0-9]|14[0-9]|15[012356789]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|)[0-9]{8}$/;
    let idcardReg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else if (this.data.phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (!telreg.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
    } else if (this.data.idcard == '') {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
    } else if (!idcardReg.test(this.data.idcard)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      })
    } else {
      wx.request({
        url: utils.dominame + '/api/reserve_project_submit',
        data: {
          project_id: that.data.id,
          token: app.globalData.token,
          reserve_count: that.data.buyNumber,
          reserve_date: that.data.date,
          ticket_user_name: that.data.name,
          ticket_user_tel: that.data.phone,
          ticket_user_card_id: that.data.idcard,
          order_source: 2
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        complete: function (res) { },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '../../bookingSuccess/bookingSuccess',
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }
  }
})